package net.zjitc.controller;

import net.zjitc.pojo.AdminUser;
import net.zjitc.pojo.User;
import net.zjitc.utils.db.C3P0Utils;
import net.zjitc.utils.dispacher.URLMapping;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

/**
 * Created by Administrator on 2017/12/24.
 */
public class ajaxController {
    @URLMapping(url = "/ajaxFindAdminUsername")
    public String ajaxFindAdminUsername(HttpServletRequest request, HttpServletResponse response){
        String username=request.getParameter("username");
        QueryRunner runner=new QueryRunner(C3P0Utils.getDataSource());
        String sql="select * from adminuser where username=?";
        try {
            AdminUser user = (AdminUser) runner.query(sql, new BeanHandler(User.class),username);
            if(user!=null){//存在
                // response.getWriter().print("<span style=\"color: red\">用户名已经存在</span>");
                response.getWriter().print(true);//给前段响应  状态
            }
            else {//不存在
                response.getWriter().print(false);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    @URLMapping(url = "/ajaxFindUsername")
    public String ajaxFindUsername(HttpServletRequest request, HttpServletResponse response){
        String username=request.getParameter("username");
        QueryRunner runner=new QueryRunner(C3P0Utils.getDataSource());
        String sql="select * from user where username=?";
        try {
            User user = (User) runner.query(sql, new BeanHandler(User.class),username);
            if(user!=null){//存在
                // response.getWriter().print("<span style=\"color: red\">用户名已经存在</span>");
                response.getWriter().print(true);//给前段响应  状态
            }
            else {//不存在
                response.getWriter().print(false);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    @URLMapping(url = "/ajaxPassword")
    public String ajaxPassword(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String password=request.getParameter("password");
        if(password=="/[a-zA-Z0-9_-]{6,12}$/"){
            response.getWriter().print(true);
        }else {
            response.getWriter().print(false);
        }
        return null;
    }
    @URLMapping(url = "/ajaxRePassword")
    public String ajaxRePassword(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String password=request.getParameter("password");
        String repassword=request.getParameter("repassword");
        if(!repassword.equals(password)){
            response.getWriter().print(true);
        }else if(repassword.equals(password)){
            response.getWriter().print(false);
        }
        return null;
    }
    @URLMapping(url = "/ajaxEmail")
    public String ajaxEmail(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String email=request.getParameter("email");
        if(email=="/[a-zA-Z0-9_-]+@([a-zA-Z0-9_-])+(\\.[a-zA-Z0-9_-]+)+/"){
            response.getWriter().print(true);
        }else {
            response.getWriter().print(false);
        }
        return null;
    }
    @URLMapping(url = "/ajaxPhone")
    public String ajaxPhone(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String phone=request.getParameter("phone");
        if(phone.startsWith("159") || phone.startsWith("139")||phone.startsWith("153")&&phone.length()==11){
            response.getWriter().print(true);
        }else {
            response.getWriter().print(false);
        }
        return null;
    }
}
